function SignRuleAdd_Code(id){
	var buffer = new StringBuffer();
	var uptime1="",downtime1="",uptime2="",downtime2="";
	if(id==0)
	{
		uptime1="08:30";
		downtime1="12:00";
		uptime2="14:00";
		downtime2="18:00";
	}
	buffer.append('<div id="signruleid" val="'+id+'" class="crmsheet border-bottom"><div class="ctitle"><div class="cname">规则名称</div><div class="require">*</div></div><div class="ctext"><input id="txt_name" type="text" placeholder="例：销售部打卡时间" value=""/></div><div class="carrow"></div></div>');
	buffer.append('<div class="crmsheet border-bottom users" show="1" touserid=""><div class="ctitle"><div class="cname">考勤员工</div><div class="require">*</div></div><div class="ctext"><input id="txt_tousers" type="text" placeholder="选择考勤员工" value="" touserid="" readonly="readonly"/></div><div class="carrow"><img src="img/arrow.png"/></div></div>');
	buffer.append('<div class="crmsheet border-bottom"><div class="ctitle"><div class="cname">考勤地点</div><div class="require">*</div></div><div class="ctext"><input id="txt_address" type="text" placeholder="点击右边定位按钮" value="" lat="0" lng="0"/></div><div id="position" class="carrow" style="margin-top: 2px;"><img id="positionicon" src="img/position.png" style="width:25px; height:25px;"/></div></div>');
	buffer.append('<div class="crmsheet border-bottom"><div class="ctitle"><div class="cname">允许误差</div><div class="require">*</div></div><div class="ctext"><input id="txt_error" type="tel" placeholder="单位：米" value=""/></div><div class="carrow"></div></div>');
	buffer.append('<div class="crmsheet border-bottom"><div class="ctitle"><div class="cname">考勤次数</div><div class="require">*</div></div><div class="ctext"><div class="time"><input id="boy" name="signcount" type="radio" style="width:16px;" checked="checked" value="2"/><label for="boy">一天2次</label></div><div class="time"><input id="girls" name="signcount" type="radio" style="width:16px;" value="4"/><label for="girls">一天4次</label></div></div><div class="carrow"></div></div>');
	buffer.append('<div class="crmsheet border-bottom"><div class="ctitle" style="width:30%;"><div class="cname">上午上班时间</div></div><div class="ctext" style="width:60%;"><div class="time utime"><input id="txt_uptime1" type="text" placeholder="上班时间" value="'+uptime1+'" readonly="readonly"/></div><div class="time utime"><input id="txt_downtime1" type="text" placeholder="下班时间" value="'+downtime1+'" readonly="readonly"/></div></div><div class="carrow"><img src="img/arrow.png"/></div></div>');
	buffer.append('<div class="crmsheet border-bottom"><div class="ctitle" style="width:30%;"><div class="cname">下午上班时间</div></div><div class="ctext" style="width:60%;"><div class="time utime"><input id="txt_uptime2" type="text" placeholder="上班时间" value="'+uptime2+'" readonly="readonly"/></div><div class="time utime"><input id="txt_downtime2" type="text" placeholder="下班时间" value="'+downtime2+'" readonly="readonly"/></div></div><div class="carrow"><img src="img/arrow.png"/></div></div>');
	buffer.append('<div class="crmsheet border-bottom"><div class="ctitle"><div class="cname">工作日设置</div></div><div id="workday" class="ctext" style="width:72%;"><div class="workday"><input type="checkbox" placeholder="" value="7"/>周日</div><div class="workday"><input type="checkbox" placeholder="" value="1" checked="checked"/>周一</div><div class="workday"><input type="checkbox" placeholder="" value="2" checked="checked"/>周二</div><div class="workday"><input type="checkbox" placeholder="" value="3" checked="checked"/>周三</div><div class="workday"><input type="checkbox" placeholder="" value="4" checked="checked"/>周四</div><div class="workday"><input type="checkbox" placeholder="" value="5" checked="checked"/>周五</div><div class="workday" style="padding-right:0px;"><input type="checkbox" placeholder="" value="6"/>周六</div></div></div>');
	if(id!=0){
	buffer.append('<div id="deletesheet" class="deletesheet" style="margin-top:50px;"><div class="name" style="width:100%; text-align:center;">删除该规则</div></div>');
	}
	return buffer.toString();
};

var map;
var wsub;
var count=0;
var ischange=false;
function Init_SignRuleAdd(){
	var signruleid=$("#signruleid").attr("val");
	
	    var data = {};
		data.act = "One_SignRule";
		data.id=signruleid;
		data.source=source;
		data.userid=userid;
		data.comid=comid;
		data.sign=GetSign(data.act);
		mui.getJSON(admin_domain, data, function (json) {
		    if (json.state) {
		    	var signrule=json.data;
		    	$("#txt_name").val(signrule.Name);
		    	$("#txt_tousers").val(signrule.ToUsersName).attr("touserid",signrule.ToUsers).parent().parent().attr("touserid",signrule.ToUsers);
		        $("#txt_address").val(signrule.Address).attr("lat",signrule.Lat).attr("lng",signrule.Lng);
		        $("#txt_error").val(signrule.Error);
		        $("input[name='signcount'][value='"+signrule.SignCount+"']").prop("checked", true);
		        $("#txt_uptime1").val(signrule.UpTime1);
		        $("#txt_downtime1").val(signrule.DownTime1);
		        $("#txt_uptime2").val(signrule.UpTime2);
		        $("#txt_downtime2").val(signrule.DownTime2);
		        if(signrule.Lat!="0")
		        {
		        	$("#positionicon").attr("src","img/aposition.png");
		        }
		        var workday = signrule.WorkDay.Trim(",").split(",");
			    if (signruleid != "0") {
			        $.each(workday, function () {
			            $("#workday input[value='" + this + "']").prop("checked", true);
			        });
			    }
		    }
		});
	
	
	
	mui(".divmsg").on("tap",".utime",function(){
			obj=this;
			var select_time = $(obj).find("input").val();
			var hour_minute=select_time.split(":");
			var t=new Date();
			if(select_time!="")
			{
				t.setHours(parseInt(hour_minute[0]), parseInt(hour_minute[1]));
			}
			plus.nativeUI.pickTime( function(e){
				var d=e.date;
				var hour=d.getHours();
				var minute=d.getMinutes();
				$(obj).find("input").val((hour<10?"0"+hour:hour)+":"+(minute<10?"0"+minute:minute));
			},function(e){
				console.log( "未选择时间："+e.message);
			},{time:t});
		});

	mui(".divmsg").on("tap",".users",function(){
			obj10=$(this);
			OpenWindow(4,"选择员工","确定",function(){
					var str1="",str2="",str3="",headimg="";
					touserid="";todepartid="";tocomid="";
	      			var count=0;
	      			mui.each(mui("#userlist input"),function(i,info){
	      				if(info.checked)
	      				{
	      					count++;
	      					touserid += info.value+",";
	      					str1 += info.getAttribute("val")+",";
	      				}
	      			});

	      			obj10.find("input").val(str1.TrimEnd(","));
	      			obj10.find("input").attr("touserid",touserid.TrimEnd(","));
					obj10.attr("touserid",touserid.TrimEnd(","));
	      			CloseWindow();
			});
			
			$("#contentlist4").html(CheckMate_Data());
			data = {};
			data.act = "DepartAndUser_Data";
			data.userid=userid;
			data.comid=comid;
			data.source=source;
			data.sign=GetSign(data.act);
			
			mui.getJSON(admin_domain, data, function(json) {
				Bind_CheckMate(json.data.user,json.data.depart);
				Init_CheckMate();
			});
			if($(this).attr("show")=="1")
	      	{
	      		$(".selectbar").hide();
	      	}else
	      	{
				$(".selectbar").show();  				
	      	}
	    });

	
	get("position").addEventListener("tap",function(){
		count++;
		ischange=false;
		OpenWindow4(200,"选择位置","确定",function(){
			map.getCurrentCenter( function ( state, point ){
					if( 0 == state ){
						$("#txt_address").attr("lat",point.latitude).attr("lng",point.longitude);
						$("#position img").attr("src","img/aposition.png");
						Get_Address(point,function(e){
							//if($("#txt_address").val()=="")
							//{
								$("#txt_address").val(e.address);
							//}
						});
						//var marker=new plus.maps.Marker(point);
						//map.addOverlay(marker);
					}else{
						alert( "Failed!" );
					}
			});
			CloseWindow();
		});
		if(count==1)
		{
			$("#contentlist200").html('<div id="map" style="height:'+(sheight-45)+'px"></div><div class="myposition"></div>');
			map=new plus.maps.Map("map");
			map.onstatuschanged=function(event){
				//wsub.loadURL('img.html?title=1');
				if(ischange){
				wsub.evalJS('Init_Address("位置更新中...")');
				map.getCurrentCenter( function ( state, point ){
					if( 0 == state ){
						Get_Address(point,function(pos){
							wsub.evalJS('Init_Address("'+pos.address+'")');
						});
						//var marker=new plus.maps.Marker(point);
						//map.addOverlay(marker);
					}else{
						alert( "Failed!" );
					}
				});
				}
			}
		}
		if($("#txt_address").attr("lat")=="0"){
					map.getUserLocation(function(state,point){
						if(0==state){
							Get_Address(point,function(pos){
							wsub.evalJS('Init_Address("'+pos.address+'")');
							});
							map.centerAndZoom(point,18);
							//var marker=new plus.maps.Marker(pos);
							//map.addOverlay(marker);
							wsub = plus.webview.create('img.html', 'sub', {
			                    top: ((sheight-45)/2)+ 20+'px',
			                    left:'0px',
			                    height: '47px',
			                    width:'100%',
			                    position: 'absolute',
			                    scrollIndicator: 'none',
			                    background: 'transparent'
			                });
			                plus.webview.currentWebview().append(wsub);
			                //map.reset();
							map.show();
							if(!ischange){
								ischange=true;
								Get_Address(point,function(poss){
									wsub.evalJS('Init_Address("'+poss.address+'")');
								});
							}
						}
					});
				}else
				{
					map.centerAndZoom(new plus.maps.Point($("#txt_address").attr("lng"),$("#txt_address").attr("lat")),18);
							//var marker=new plus.maps.Marker(pos);
							//map.addOverlay(marker);
							wsub = plus.webview.create('img.html', 'sub', {
			                    top: ((sheight-45)/2)+ 20+'px',
			                    left: '0px',
			                    height: '47px',
			                    width:'100%',
			                    position: 'absolute',
			                    scrollIndicator: 'none',
			                    background: 'transparent'
			                });
			                plus.webview.currentWebview().append(wsub);
			                //map.reset();
							map.show();
							if(!ischange){
								ischange=true;
								Get_Address(new plus.maps.Point($("#txt_address").attr("lng"),$("#txt_address").attr("lat")),function(pos){
									wsub.evalJS('Init_Address("'+pos.address+'")');
								});
							}
				}
		
	});
	
	if($("#signruleid").attr("val")!="0")
		{
			get("deletesheet").addEventListener("tap",function(){
		        	plus.nativeUI.confirm("确定要删除吗？", function(event){
						if(event.index==1)
						{
							var data = {};
						    data.act = "Delete_SignRule";
						    data.ids = $("#signruleid").attr("val");
						    data.userid=userid;
							data.comid=comid;
							data.source=source;
							data.sign=GetSign(data.act);
						    mui.getJSON(admin_domain, data, function(json) {
								if(json.state)
								{
									obj.hide(300);
									CloseWindow();
									plus.nativeUI.toast(json.msg);
								}else
								{
									plus.nativeUI.alert(json.msg);
								}
								
						    });	
						}
					},"提示", ["取消","确认"]); 
				
			});
		}
	
};


function Save_SignRule() {
        var data = {};
        var txt_name = $("#txt_name");
        var txt_address = $("#txt_address");
        var txt_error = $("#txt_error");
        var tousers = $("#txt_tousers").attr("touserid");;
        var txt_uptime1 = $("#txt_uptime1");
        var txt_downtime1 = $("#txt_downtime1");
        var txt_uptime2 = $("#txt_uptime2");
        var txt_downtime2 = $("#txt_downtime2");
        var workday = CRM.ItemID("#workday") + "";
        data.id = $("#signruleid").attr("val");
        if(txt_name.val().Trim()=="")
        {
        	plus.nativeUI.toast("请输入规则名称");
        	return false;
        }
        if(tousers.Trim()=="")
        {
        	plus.nativeUI.toast("请选择员工");
        	return false;
        }
        if(txt_address.val().Trim()=="")
        {
        	plus.nativeUI.toast("请输入考勤地点");
        	return false;
        }
        if(txt_error.val().Trim()=="")
        {
        	plus.nativeUI.toast("请输入允许误差");
        	return false;
        }
        if(workday.Trim()=="")
        {
        	plus.nativeUI.toast("请选择工作日");
        	return false;
        }
		data.act = "Save_SignRule";
		data.name = txt_name.val();
        data.tousers = tousers;
        data.address = txt_address.val();
        data.lat = txt_address.attr("lat");
        data.lng = txt_address.attr("lng");
        data.error = txt_error.val();
        data.signcount = $("input[name='signcount']:checked").val();
        data.uptime1 = txt_uptime1.val();
        data.downtime1 = txt_downtime1.val();
        data.uptime2 = txt_uptime2.val();
        data.downtime2 = txt_downtime2.val();
        data.workday = workday;
        data.userid=userid;
		data.comid=comid;
        data.source = source; 
        data.version = version;
        data.sign=GetSign(data.act);
        mui.getJSON(admin_domain, data, function (json) {
        	plus.nativeUI.toast(json.msg); 
            if (json.state) {
                CloseWindow();
                Receive_SignRule(1);
                if($("#iframe .title .name").html()=="消息"){ //如果是通过消息页面进行设置的，执行下面代码
                	Init_Notice();
                }
            }
        });
};
